<template>
  <div v-loading="loading">
    <header>
      <div class="header-left">
        <el-icon>
          <HelpFilled />
        </el-icon>
        <span class="header-title"><strong>{{ profileData.name }}</strong></span>
      </div>
      <el-icon v-if="IsDepartmentMember" class="header-icon" @click="enterManagePage" size="20px">
        <Edit />
      </el-icon>
    </header>
    <main>
      <!-- 部门介绍 -->
      <div class="info-container">
        <el-descriptions class="descriptions-list" size="large" border>
          <template #title>
            <div class="descriptions-title">
              <el-icon class="descriptions-title-icon">
                <Cpu />
              </el-icon>
              <span>部门介绍</span>
            </div>
          </template>

        </el-descriptions>
        <div v-html="departmentDetail" class="project-introduction"></div>
      </div>
      <!-- 部门成员 -->
      <div class="info-container">
        <el-descriptions class="descriptions-list" size="large" border>
          <template #title>
            <div class="descriptions-title">
              <el-icon class="descriptions-title-icon">
                <User />
              </el-icon>
              <span>部门成员</span>
            </div>
          </template>
        </el-descriptions>
        <el-table :data="profileData.members" border>
          <el-table-column prop="id" label="ID">
            <template v-slot="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="" label="姓名">
            <template v-slot="scope">
              <span>{{ scope.row.name }}</span>
              <el-tag v-if="scope.row.role.name !== '成员'" style="margin-left: 10px;">{{ scope.row.role.name }}</el-tag>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="name" label="部门身份">
          <template v-slot="scope">
            <span>{{ scope.row.role.name }}</span>
          </template>
        </el-table-column> -->
          <el-table-column prop="annotation" label="备注">
            <template v-slot="scope">
              <span>{{ scope.row.annotation }}</span>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <!-- 部门项目 -->
      <div class="info-container">
        <el-descriptions class="descriptions-list" size="large" border>
          <template #title>
            <div class="descriptions-title">
              <el-icon class="descriptions-title-icon">
                <el-icon>
                  <FolderChecked />
                </el-icon>
              </el-icon>
              <span>项目</span>
            </div>
          </template>
        </el-descriptions>
        <el-table :data="profileData.projects" border>
          <el-table-column prop="id" label="ID">
            <template v-slot="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="项目名称">
            <template v-slot="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>

        </el-table>
      </div>
    </main>
  </div>
  <!-- <Loading :isLoading="loading" /> -->
</template>

<script>
import { post } from '../../utils/common/api'
import MarkdownIt from 'markdown-it'

export default {
  data() {
    return {
      id: this.$route.params.id,
      profileData: {
        id: undefined,
        name: '',
        detail: '',
        members: [
          {
            id: undefined,
            name: '',
            annotation: '',
            role: {
              id: undefined,
              name: '',//人员的部门身份称谓
            }
          },
        ],
        projects: [
        ]
      },
      loading: false,
    }
  },
  computed: {
    departmentDetail() {
      const md = new MarkdownIt()
      const result = md.render(this.profileData.detail)
      return result
    },
    IsDepartmentMember() {
      const members = this.profileData.members
      return members.map(item => item.id).includes(+this.usePermission.id)
    },
  },
  methods: {
    async getDepartmentProfile() {
      this.loading = true;
      const res = await post('/department/profile', {
        id: this.id
      })
      this.profileData = res;
      this.loading = false;
    },
    enterManagePage() {
      this.$router.push({
        path: `/department/edit/${this.id}`
      })
    }
  },
  mounted() {
    this.getDepartmentProfile()
  },
}
</script>

<style scoped>
header {
  height: 40px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  margin-left: 5px;
  display: flex;
  align-items: center;
}

.header-title {
  font-size: 20px;
  margin-left: 8px;
}

.header-icon {
  margin-right: 20px;
  cursor: pointer;
  color: rgb(252, 85, 49);
}

main {
  width: 100%;
}

.project-introduction {
  padding: 24px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  text-align: left;
  /* font-size: 15px; */
  box-sizing: border-box;
}

.info-container {
  width: 75%;
  margin: 20px auto;
  /* margin-top: 20px; */
}

.descriptions-title {
  display: flex;
  align-items: center;
}

.descriptions-title-icon {
  margin-right: 7px;
}
</style>